

package org.lq.ht.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.lq.ht.dao.OrderSheetDao;
import org.lq.ht.entity.OrderSheet;
import org.lq.ht.util.Instantiation;
import org.lq.ht.util.JDBCUtil;

/**
 * 订单进货表实现类
 * @author 孙少阳
 * @phone 15936587261
 *
 * @package org.lq.ht.dao.impl
 * @date 2020年8月20日下午11:29:06
 */
public class OrderSheetDaoImpl  implements OrderSheetDao,Instantiation<OrderSheet>{

	@Override
	public int save(OrderSheet t) {
		String sql = "insert into order_sheet(order_number,goods_number,"
				+ "name,goods_type,purchase,count,purchase_sum,"
				+ "order_date,supplier,note)values(?,?,?,?,?,?,?,?,?,?)";
		try {
			return JDBCUtil.executeUpdate(sql, t.getOrderNumber(),t.getGoodsNumber(),t.getName(),
					t.getGoodsType(),t.getPurchase(),t.getCount(),t.getPurchaseSum(),
					t.getOrderDate(),t.getSupplier(),t.getNote()
					);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int update(OrderSheet t) {
		String sql = "update  order_sheet set order_number = ?,goods_number= ?,"
				+ "name= ?,goods_type= ?,purchase= ?,count= ?,purchase_sum= ?,"
				+ "order_date= ?,supplier= ?,note= ? where no = ?";
			try {
				return JDBCUtil.executeUpdate(sql, t.getOrderNumber(),
						t.getGoodsNumber(),t.getName(),
						t.getGoodsType(),t.getPurchase(),t.getCount(),t.getPurchaseSum(),
						t.getOrderDate(),t.getSupplier(),t.getNote(),t.getNo());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
	}

	@Override
	public int delete(int id) {
		String sql = "delete from order_sheet where no = ?";
		try {
			return JDBCUtil.executeUpdate(sql, id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<OrderSheet> findAll() {
		try {
			return JDBCUtil.executeQuery("select *from order_sheet",this);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public OrderSheet getById(int id) {
		try {
			List<OrderSheet> list = JDBCUtil.executeQuery("select *from order_sheet where no = ?", this, id);
			return list.get(0);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public OrderSheet instance(ResultSet rs) {
		OrderSheet os = new OrderSheet();
		try {
			os.setNo(rs.getInt("no"));
			os.setOrderNumber(rs.getInt("order_number"));
			os.setGoodsNumber(rs.getInt("goods_number"));
			os.setName(rs.getString("name"));
			os.setGoodsType(rs.getInt("goods_type"));
			os.setPurchase(rs.getDouble("purchase"));
			os.setCount(rs.getInt("count"));
			os.setPurchaseSum(rs.getDouble("purchase_sum"));
//			os.setOrderDate(rs.getTimestamp("order_date"));
			os.setSupplier(rs.getInt("supplier"));
			os.setNote(rs.getString("note"));
			return os;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
}

